package com.rtx.wms.std.common.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.rtx.api.annotation.DataSourceAnnotation;
import com.rtx.model.emums.SysNumberType;
import com.rtx.wms.std.common.bean.Codelkup;
import com.rtx.wms.std.common.bean.PlDb;
import com.rtx.wms.std.common.mapper.CodelkupMapper;
import com.rtx.wms.std.common.mapper.CommonMapper;
import com.rtx.wms.std.common.service.CommonService;
import com.rtx.wms.std.sku.bean.po.Sku;
import com.rtx.wms.std.sku.mapper.SkuMapper;

import cn.hutool.http.HttpException;
import cn.hutool.http.HttpRequest;

@Service
public class CommonServiceImpl implements CommonService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Value("${wms.app.sysnumberrule.url}")
    private String sysnumberrule_url;

    @Autowired
    private CodelkupMapper codelkupMapper;

    @Autowired
    private CommonMapper commonMapper;

    @Autowired
    private SkuMapper skuMapper;

    @DataSourceAnnotation
    @Override
    public List<String> selectActiveWhseid(String whseid) {
        return commonMapper.selectActiveWhseid();
    }

    @DataSourceAnnotation
    @Override
    public List<String> selectActiveWhseidNotEnterprise(String whseid) {
        return commonMapper.selectActiveWhseidNotEnterprise();
    }

    @DataSourceAnnotation
    @Override
    public PlDb selectActiveWhseidByDblogid(String whseid, String db_logid) {
        return commonMapper.selectActiveWhseidByDblogid(db_logid);
    }

    @DataSourceAnnotation
    @Override
    public Codelkup selectCodelkupByCodeAndListname(String whseid,String code, String listname) {
        LambdaQueryWrapper<Codelkup> query = Wrappers.lambdaQuery();
        query.eq(Codelkup::getCode,code).eq(Codelkup::getListname,listname);
        return codelkupMapper.selectOne(query);
    }

    @DataSourceAnnotation
    @Override
    public List<Codelkup> selectCodelkupByWrapper(String whseid,Wrapper<Codelkup> wrapper) {
        return codelkupMapper.selectList(wrapper);
    }

    @DataSourceAnnotation
    @Override
    public Sku selectSkuByWrapper(String whseid, Wrapper<Sku> wrapper) {
        return skuMapper.selectOne(wrapper);
    }

    @DataSourceAnnotation
    @Override
    public String getWmsKey(String whseid,String prefix, String keyname) {
        String wmskey = null;
        Integer count = commonMapper.selectWmskey(keyname);
        if(count == null){
            count = 1;
            commonMapper.insertNcounter(keyname);
        }else{
            commonMapper.updateNcounter(keyname);
        }
        if(StringUtils.hasText(prefix)){
            if(prefix.length() > 2){
                prefix = prefix.substring(0,2);
            }
            wmskey = prefix + String.format("%0" + (10 - prefix.length()) + "d",count);
        }else{
            wmskey = String.format("%010d",count);
        }
        return wmskey;
    }

    @Override
    public String getWmsKey(String whseid, SysNumberType sysNumberType) {
        try {
        	String requesturl = sysnumberrule_url + whseid.toUpperCase() + "/" + sysNumberType.getValue();
            String body = HttpRequest
                            .get(requesturl)
                            .timeout(30000).execute()
                            .body();
            logger.info("请求地址：{}，获取WMS单号结果：{}",requesturl,body);
            //{"returnCode":"200","returnMgr":"SUCCESS","data":"SO2304250026","total":0,"token":null}
            if(StringUtils.hasText(body)){
                JSONObject jsonObject = JSON.parseObject(body);
                if("200".equals(jsonObject.getString("returnCode"))){
                    return jsonObject.getString("data");
                }
            }
        } catch (HttpException e) {
            logger.error("请求地址：{}，获取WMS单号异常：",sysnumberrule_url,e);
        }
        return null;
    }

    @DataSourceAnnotation
    @Override
    public Object executeSql(String whseid, String sql) {
        return commonMapper.executeSql(sql);
    }
}
